package com.easondongh.business.controller;

import com.easondongh.business.domain.Goods;
import com.easondongh.business.domain.GoodsSales;
import com.easondongh.business.service.GoodsSalesService;
import com.easondongh.business.service.GoodsService;
import com.easondongh.business.vo.GoodsSalesQueryVo;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.domain.SysUser;
import com.easondongh.common.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/6/8 15:30
 */
@Slf4j
@RestController
@RequestMapping("/goodsSales")
public class GoodsSalesController {

    @Autowired
    private GoodsSalesService goodsSalesGoodsSalesService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 新增商品销售
     * @param goodsSales
     * @return
     */
    @PostMapping("/addGoodsSales")
    public ResponseObj addGoodsSales(@RequestBody GoodsSales goodsSales) {
        if (goodsSales == null || goodsSales.getGoodsId() == null || goodsSales.getCustomerId() == null
                || goodsSales.getNumber() == null) {
            log.warn("商品销售：新增时商品id、客户id、商品数量均不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        Goods goodsById = goodsService.getById(goodsSales.getGoodsId());
        if(goodsById == null) {
            log.warn("商品销售：新增时商品找不到，GoodsId：{}",goodsSales.getGoodsId());
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        int diff = goodsById.getNumber() - goodsSales.getNumber();
        if(diff < 0) {
            log.warn("商品销售：新增时商品库存不足");
            return new ResponseObj(ResponseEnum.GOODS_NOT_ENOUGH);
        }
        goodsSales.setSalesTime(new Date());
        SysUser loginUser = ShiroUtils.getCurrentLoginUser();
        goodsSales.setOperatePerson(loginUser.getName());
        this.goodsSalesGoodsSalesService.addGoodsSales(goodsSales);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 批量删除商品销售
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.goodsSalesGoodsSalesService.deleteByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 分页查询商品销售
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<GoodsSales>> getByPage(@RequestBody BasePageQueryVo<GoodsSalesQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<GoodsSales> result = this.goodsSalesGoodsSalesService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }
}
